package com.cloud.ebookreader.opf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.cloud.ebookreader.access.Component;

public class InMemoryComponent implements Component{
	String name;
    byte[] data;

    private String mime;
    
    Logger logger = Logger.getLogger(this.getClass());
    
    public InMemoryComponent(){
        
    }
    
    public InMemoryComponent(String name, InputStream zip) throws IOException {
        this.name = name;
        loadFromStream(zip);
    }
    
    public void loadFromStream(InputStream in) throws IOException{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        data = bout.toByteArray();
        logger.info("Build In Memory Component with "+data.length+" bytes of data");
    }

    public void setName(String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public InputStream getContent() {
        return new ByteArrayInputStream(data);
    }

    @Override
    public String toString() {
        return "[ In Memory Component : "+name+" using "+data.length+" bytes of ram ]";
    }

    /**
     * @return the mime
     */
    public String getMime() {
        return mime;
    }

    /**
     * @param mime the mime to set
     */
    public void setMime(String mime) {
        this.mime = mime;
    }
}
